﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 04.04.2008
 * Zeit: 19:25
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Collections.Generic;

namespace Gamadus.Engine
{
	/// <summary>
	/// Description of ObjectSuper.
	/// </summary>
	public class Object : Resource
	{
		ObjectVarList variable;
		
		ObjectSuper firstInstance;
		
		public ObjectSuper FirstInstance
		{
			get
			{
				return firstInstance;
			}
		}
		
		public ObjectVarList Variable
		{
			get
			{
				return variable;
			}
		}
		
		List<ObjectSuper> instances = new List<ObjectSuper>();
		public IEnumerable<ObjectSuper> Instances
		{
			get
			{
				return instances;
			}
		}
		
		Type type;
		public Object(int id, Type type) : base(id)
		{
			if(!type.IsSubclassOf(typeof(ObjectSuper)))
			    throw new ArgumentException("Given type is not a valid Object class");
			
			this.type = type;
			
			variable = new ObjectVarList(this);
		}
		
		public void DestroyInstance(ObjectSuper inst)
		{
			instances.Remove(inst);
			if(firstInstance == inst)
				firstInstance = (instances.Count > 0) ? instances[0] : null;
		}
		
		public ObjectSuper CreateInstance(int id, double x, double y, Room room)
		{
			Console.WriteLine("Creating instance of type "+type.Name);
			if(room == null)
			    throw new ArgumentNullException("Given Room is not initialised");
			
			ObjectSuper inst = (ObjectSuper)System.Activator.CreateInstance(type, id, x, y, room);
			if(firstInstance == null)
				firstInstance = inst;
			instances.Add(inst);
			return inst;
		}

	}
}
